﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;

namespace AllenalaMente.State
{
    /// <summary>
    /// Provides functions to access data saved on disk
    /// </summary>
    public static class SaveUtils
    {      
        /// <summary>
        /// Saves the game
        /// </summary>
        /// <param name="game">The current game</param>
        /// <param name="config">The game to save</param>
        static public void SaveGame()
        {
            GameState savedState = State.GameStateManager.GetState();

            savedState.hasSavedGame = true;

            XmlSerializer serializer = new XmlSerializer(typeof(GameState));

            string saveGameFile = "SavedGame.car";

            IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();

            // Replace the existing saved game with the new one
            if (appStorage.FileExists(saveGameFile))
                appStorage.DeleteFile(saveGameFile);

            IsolatedStorageFileStream streamFile = appStorage.OpenFile(saveGameFile, FileMode.CreateNew);
            StreamWriter writer = new StreamWriter(streamFile);

            serializer.Serialize(writer, savedState);
            writer.Close();
        }

        /// <summary>
        /// Loads a saved game
        /// </summary>
        /// <param name="game">The current game</param>
        /// <param name="id">The id number of the user</param>
        static public GameState LoadGame()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(GameState));
            string savedGameFile = "SavedGame.car";

            IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();

            if (!appStorage.FileExists(savedGameFile))
                return null;

            IsolatedStorageFileStream filestream = appStorage.OpenFile(savedGameFile, FileMode.Open, FileAccess.Read);

            GameState savedGame = (GameState)serializer.Deserialize(filestream);
            filestream.Close();
            return savedGame;
        }

        /// <summary>
        /// Deletes a saved game
        /// </summary>
        /// <param name="game">The current game instance</param>
        static public void DeleteGame()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(GameState));

            string saveGameFile = "SavedGame.car";

            IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();
            // If the requested saved game doesn't exists, return
            if (!appStorage.FileExists(saveGameFile))
                return;
            appStorage.DeleteFile(saveGameFile);
        }
    }
}
